﻿//getAfterBufferGeometry(缓冲图形, 缓冲距离);//返回缓冲后的面>>>>>>>>ol.js   jsts.js
//changeCoord(坐标数组, 旧坐标系, 新坐标系);//坐标系 like EPSG:3857   ########ol.js
//getGeometryByCoord(坐标,是否点数组,要转成的要素null/point/polyline/polygon);

//获取缓冲结果基于jsts
function getAfterBufferGeometry(geom, distance) {
    //要进行缓冲的图形
    var feature = new ol.Feature({
        geometry: geom
    });
    var parser = new jsts.io.OL3Parser();
    parser.inject(ol.geom.Point, ol.geom.LineString, ol.geom.LinearRing, ol.geom.Polygon);
    var bufferGeom = parser.read(feature.getGeometry());
    var bufferedGeom = bufferGeom.buffer(distance);
    var geomtryArr = bufferedGeom._shell._points._coordinates;//缓冲后得到的坐标

    var polygonarr = new Array();
    for (var i = 0; i < geomtryArr.length; i++) {
        var pointObj = geomtryArr[i];
        polygonarr.push([pointObj["x"], pointObj["y"]]);
    }

    var polygon = new ol.geom.Polygon([polygonarr]);
    return polygon;
}
//坐标转换
function changeCoord(coordarr, oldSR, newSR) {
    var newcoordarr = new Array();
    for (var i = 0; i < coordarr.length; i++) {
        newcoordarr.push(ol.proj.transform(coordarr[i], oldSR, newSR));
    }
    return newcoordarr;
}
//坐标串转图形
function getGeometryByCoord(coord, isPointArr, toType) {
    var thisResult = null;
    var geometryArr = new Array();
    if (isPointArr) {
        geometryArr = coord;
    }
    else {
        if (typeof(coord)=="string") {
            coord = coord.split(",");
        }
        for (var i = 0; i < (coord.length) / 2; i++) {
            var geometry = new Array();
            geometry.push(parseFloat(coord[(2 * i)]), parseFloat(coord[(2 * i + 1)]));
            geometryArr.push(geometry);
        }
    }
    switch (toType) {
        case "point":
            thisResult = ol.geom.Point(geometryArr[0]);
            break;
        case "polyline":
            thisResult = ol.geom.LineString(geometryArr);
            break;
        case "polygon":
            thisResult = ol.geom.Polygon([geometryArr]);
            break;
        default:
            thisResult=geometryArr;
            break;
    }
    return thisResult;
}